{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Compare Version Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: compareVersion"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #比较版本号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个版本号 <code>version1</code> 和 <code>version2</code> ，请你比较它们。</p>\n",
    "\n",
    "<p>版本号由一个或多个修订号组成，各修订号由一个 <code>'.'</code> 连接。每个修订号由 <strong>多位数字</strong> 组成，可能包含 <strong>前导零</strong> 。每个版本号至少包含一个字符。修订号从左到右编号，下标从 0 开始，最左边的修订号下标为 0 ，下一个修订号下标为 1 ，以此类推。例如，<code>2.5.33</code> 和 <code>0.1</code> 都是有效的版本号。</p>\n",
    "\n",
    "<p>比较版本号时，请按从左到右的顺序依次比较它们的修订号。比较修订号时，只需比较 <strong>忽略任何前导零后的整数值</strong> 。也就是说，修订号 <code>1</code> 和修订号 <code>001</code> <strong>相等 </strong>。如果版本号没有指定某个下标处的修订号，则该修订号视为 <code>0</code> 。例如，版本 <code>1.0</code> 小于版本 <code>1.1</code> ，因为它们下标为 <code>0</code> 的修订号相同，而下标为 <code>1</code> 的修订号分别为 <code>0</code> 和 <code>1</code> ，<code>0 &lt; 1</code> 。</p>\n",
    "\n",
    "<p>返回规则如下：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果&nbsp;<code><em>version1&nbsp;</em>&gt;&nbsp;<em>version2</em></code>&nbsp;返回&nbsp;<code>1</code>，</li>\n",
    "\t<li>如果&nbsp;<code><em>version1&nbsp;</em>&lt;&nbsp;<em>version2</em></code> 返回 <code>-1</code>，</li>\n",
    "\t<li>除此之外返回 <code>0</code>。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>version1 = \"1.01\", version2 = \"1.001\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>忽略前导零，\"01\" 和 \"001\" 都表示相同的整数 \"1\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>version1 = \"1.0\", version2 = \"1.0.0\"\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>version1 没有指定下标为 2 的修订号，即视为 \"0\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>version1 = \"0.1\", version2 = \"1.1\"\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>version1 中下标为 0 的修订号是 \"0\"，version2 中下标为 0 的修订号是 \"1\" 。0 &lt; 1，所以 version1 &lt; version2\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= version1.length, version2.length &lt;= 500</code></li>\n",
    "\t<li><code>version1</code> 和 <code>version2</code> 仅包含数字和 <code>'.'</code></li>\n",
    "\t<li><code>version1</code> 和 <code>version2</code> 都是 <strong>有效版本号</strong></li>\n",
    "\t<li><code>version1</code> 和 <code>version2</code> 的所有修订号都可以存储在 <strong>32 位整数</strong> 中</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [compare-version-numbers](https://leetcode.cn/problems/compare-version-numbers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [compare-version-numbers](https://leetcode.cn/problems/compare-version-numbers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1.01\"\\n\"1.001\"', '\"1.0\"\\n\"1.0.0\"', '\"0.1\"\\n\"1.1\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1, version2):\n",
    "        # 切分，补齐\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2) # 转int\n",
    "            if x != y: # 比较\n",
    "                return 1 if x > y else -1\n",
    "\n",
    "        return 0 # 相同版本返回0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1, v2 = list(map(int, version1.split('.'))), list(map(int, version2.split('.')))\n",
    "        l1, l2 = len(v1), len(v2)\n",
    "        if l1 < l2: v1 += [0] * (l2 - l1)\n",
    "        else:       v2 += [0] * (l1 - l2)\n",
    "        return 1 if v1 > v2 else -1 if v1 < v2 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        nums1, nums2 = list(map(int, version1.split('.'))), list(map(int, version2.split('.')))\n",
    "        p1 = 0\n",
    "        while p1 < len(nums1) and p1 < len(nums2):\n",
    "            if nums1[p1] < nums2[p1]:\n",
    "                return -1\n",
    "            if nums1[p1] > nums2[p1]:\n",
    "                return 1\n",
    "            p1 += 1\n",
    "        if p1 < len(nums2):\n",
    "            return -1 if sum(nums2[p1:]) else 0\n",
    "        if p1 < len(nums1):\n",
    "            return 1 if sum(nums1[p1:]) else 0\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "\n",
    "        version1 = version1.split('.')\n",
    "        version2 = version2.split('.')\n",
    "\n",
    "        for a,b in zip_longest(version1,version2,fillvalue=0):\n",
    "            if int(a)>int(b):\n",
    "                return 1\n",
    "            elif int(a)<int(b):\n",
    "                return -1\n",
    "\n",
    "        return 0\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "\n",
    "        m, n = len(version1), len(version2)\n",
    "        i, j = 0, 0\n",
    "\n",
    "        while i<m or j<n:\n",
    "            # 处理第一个字符串\n",
    "            x = 0\n",
    "            while i<m and version1[i] != '.':\n",
    "                x = x*10 + int(version1[i])\n",
    "                i += 1\n",
    "            i += 1\n",
    "            y = 0\n",
    "            while j<n and version2[j] != '.':\n",
    "                y = y*10 + int(version2[j])\n",
    "                j += 1\n",
    "            j += 1\n",
    "\n",
    "            if x>y:\n",
    "                return 1\n",
    "            elif x<y:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        version1 = [int(i) for i in version1.split(\".\")]\n",
    "        version2 = [int(i) for i in version2.split(\".\")]\n",
    "        length = min(len(version1), len(version2))\n",
    "        for i in range(length):\n",
    "            v1 = version1.pop(0)\n",
    "            v2 = version2.pop(0)\n",
    "            if v1 > v2:\n",
    "                return 1\n",
    "            elif v1 < v2:\n",
    "                return -1\n",
    "        if sum(version1) > 0:\n",
    "            return 1\n",
    "        elif sum(version2) > 0:\n",
    "            return -1\n",
    "        return 0\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        \n",
    "        for idx in range(max(len(v1), len(v2))): \n",
    "            val1 = int(v1[idx]) if idx < len(v1) else 0 \n",
    "            val2 = int(v2[idx]) if idx < len(v2) else 0 \n",
    "\n",
    "            if val1 < val2:\n",
    "                return -1 \n",
    "            elif val1 > val2:\n",
    "                return 1 \n",
    "\n",
    "        return 0 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        if len(v1) > len(v2):\n",
    "            return -self.compareVersion(version2, version1)\n",
    "\n",
    "        for i in range(min(len(v2), len(v1))):\n",
    "            if int(v1[i]) < int(v2[i]):\n",
    "                return -1\n",
    "            elif int(v1[i]) > int(v2[i]):\n",
    "                return 1\n",
    "            else:\n",
    "                continue\n",
    "\n",
    "        if len(v1) == len(v2):\n",
    "            return 0\n",
    "        for i in range(len(v1), len(v2)):\n",
    "            if int(v2[i]) != 0:\n",
    "                return -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        n, m = len(version1), len(version2)\n",
    "        i, j = 0, 0\n",
    "        while i < n or j < m:\n",
    "            x = 0\n",
    "            while i < n and version1[i] != '.':\n",
    "                x = x * 10 + ord(version1[i]) - ord('0')\n",
    "                i += 1\n",
    "            i += 1  # 跳过点号\n",
    "            y = 0\n",
    "            while j < m and version2[j] != '.':\n",
    "                y = y * 10 + ord(version2[j]) - ord('0')\n",
    "                j += 1\n",
    "            j += 1  # 跳过点号\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        \"\"\"\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x,y=int(v1),int(v2)\n",
    "            if x!=y:\n",
    "                return 1 if x>y else -1\n",
    "        return 0\n",
    "        \"\"\"\n",
    "        n, m = len(version1), len(version2)\n",
    "        i, j = 0, 0\n",
    "        while i < n or j < m:\n",
    "            x = 0\n",
    "            while i < n and version1[i] != '.':\n",
    "                x = x * 10 + ord(version1[i]) - ord('0')\n",
    "                i += 1\n",
    "            i += 1  # 跳过点号\n",
    "            y = 0\n",
    "            while j < m and version2[j] != '.':\n",
    "                y = y * 10 + ord(version2[j]) - ord('0')\n",
    "                j += 1\n",
    "            j += 1  # 跳过点号\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n",
    "\n",
    "\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "\n",
    "        n1 = len(v1)\n",
    "        n2 = len(v2)\n",
    "\n",
    "        for i in range(min(n1,n2)):\n",
    "            if int(v1[i]) > int(v2[i]):\n",
    "                return 1\n",
    "            elif int(v1[i]) < int(v2[i]):\n",
    "                return -1\n",
    "        \n",
    "        if n1==n2:\n",
    "            return 0\n",
    "        if n1>n2:\n",
    "            for i in range(n2,n1):\n",
    "                if int(v1[i])!=0:\n",
    "                    return 1\n",
    "        if n1<n2:\n",
    "            for i in range(n1,n2):\n",
    "                if int(v2[i])!=0:\n",
    "                    return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        m, n = len(version1), len(version2)\n",
    "        i = j = 0\n",
    "        while i < m or j < n:\n",
    "            a = b = 0\n",
    "            while i < m and version1[i] != '.':\n",
    "                a = 10 * a + int(version1[i])\n",
    "                i += 1\n",
    "            while j < n and version2[j] != '.':\n",
    "                b = 10 * b + int(version2[j])\n",
    "                j += 1\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return 0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        m, n = len(v1), len(v2)\n",
    "        for i in range(max(n, m)):\n",
    "            if i < m:\n",
    "                num1 = int(v1[i])\n",
    "            else:\n",
    "                num1 = 0\n",
    "            if i < n:\n",
    "                num2 = int(v2[i])\n",
    "            else:\n",
    "                num2 = 0\n",
    "            if num1 > num2:\n",
    "                return 1\n",
    "            if num1 < num2:\n",
    "                return -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1, version2):\n",
    "        # 切分，补齐\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2) # 转int\n",
    "            if x != y: # 比较\n",
    "                return 1 if x > y else -1\n",
    "\n",
    "        return 0 # 相同版本返回0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1_list = version1.split('.')\n",
    "        v2_list = version2.split('.')\n",
    "        len1 = len(v1_list)\n",
    "        len2 = len(v2_list)\n",
    "        i = 0\n",
    "        j = 0\n",
    "        while i < len1 and j < len2:\n",
    "            if int(v1_list[i]) > int(v2_list[j]):\n",
    "                return 1\n",
    "            elif int(v1_list[i]) < int(v2_list[j]):\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        if i < len1:\n",
    "            while i < len1:\n",
    "                if int(v1_list[i]) > 0:\n",
    "                    return 1\n",
    "                i += 1\n",
    "        if j < len2:\n",
    "            while j < len2:\n",
    "                if int(v2_list[j]) > 0:\n",
    "                    return -1\n",
    "                j += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        n1, n2 = len(v1), len(v2)\n",
    "        max_n = max(n1, n2)\n",
    "        for i in range(max_n):\n",
    "            v1_t = 0\n",
    "            if i < n1:\n",
    "                v1_t = int(v1[i])\n",
    "            v2_t = 0\n",
    "            if i < n2:\n",
    "                v2_t = int(v2[i])\n",
    "            if v1_t > v2_t:\n",
    "                return 1\n",
    "            elif v2_t > v1_t:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ver1 = [int(seg) for seg in version1.split(\".\")]\n",
    "        ver2 = [int(seg) for seg in version2.split(\".\")]\n",
    "\n",
    "        size = max(len(ver1), len(ver2))\n",
    "        for i in range(size):\n",
    "            v1 = ver1[i] if i < len(ver1) else 0\n",
    "            v2 = ver2[i] if i < len(ver2) else 0\n",
    "\n",
    "            if v1 > v2:\n",
    "                return 1\n",
    "            elif v1 < v2:\n",
    "                return -1\n",
    "        \n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        m, n = len(version1), len(version2)\n",
    "        i = j = 0\n",
    "        while i < m or j < n:\n",
    "            a = b = 0\n",
    "            while i < m and version1[i] != '.':\n",
    "                a = 10 * a + int(version1[i])\n",
    "                i += 1\n",
    "            while j < n and version2[j] != '.':\n",
    "                b = 10 * b + int(version2[j])\n",
    "                j += 1\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = [int(x) for x in version1.split('.')]\n",
    "        v2 = [int(x) for x in version2.split('.')]\n",
    "        len1, len2 = len(v1), len(v2)\n",
    "        max_len = max(len1, len2)\n",
    "        v1.extend([0] * (max_len - len1))\n",
    "        v2.extend([0] * (max_len - len2))\n",
    "        for i in range(max_len):\n",
    "            if v1[i] > v2[i]:\n",
    "                return 1\n",
    "            elif v1[i] < v2[i]:\n",
    "                return -1\n",
    "        return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        l1 = version1.split('.')\n",
    "        l2 = version2.split('.')\n",
    "        for v1,v2 in zip_longest(l1,l2,fillvalue=0):\n",
    "            x1,x2 = int(v1),int(v2)\n",
    "            if x1 != x2: return int((x1-x2)/abs(x1-x2))\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        m, n = len(version1), len(version2)\n",
    "        i = j = 0\n",
    "        while i < m or j < n:\n",
    "            a = b = 0\n",
    "            while i < m and version1[i] != '.':\n",
    "                a = 10 * a + int(version1[i])\n",
    "                i += 1\n",
    "            while j < n and version2[j] != '.':\n",
    "                b = 10 * b + int(version2[j])\n",
    "                j += 1\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1,v2 in zip_longest(version1.split('.'),version2.split('.'),fillvalue=0):\n",
    "            x,y=int(v1),int(v2)\n",
    "            if x!=y:\n",
    "                return 1 if x>y else -1\n",
    "        return 0\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1, version2):\n",
    "        # 切分，补齐\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2) # 转int\n",
    "            if x != y: # 比较\n",
    "                return 1 if x > y else -1\n",
    "\n",
    "        return 0 # 相同版本返回0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1, version2):\n",
    "        # 切分，补齐\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2) # 转int\n",
    "            if x != y: # 比较\n",
    "                return 1 if x > y else -1\n",
    "\n",
    "        return 0 # 相同版本返回0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def compareVersion(self, version1: str, version2: str) -> int:\n",
    "#         v1 = version1.split('.')\n",
    "#         v2 = version2.split('.')\n",
    "#         n = min(len(v1), len(v2))\n",
    "#         while v1 and v2:\n",
    "#             cur1, cur2 = int(v1.pop(0)), int(v2.pop(0))\n",
    "#             if cur1 > cur2:\n",
    "#                 return 1\n",
    "#             elif cur1 < cur2:\n",
    "#                 return -1\n",
    "#         if v1 and int(''.join(v1)) > 0:\n",
    "#             return 1\n",
    "#         if v2 and int(''.join(v2)) > 0:\n",
    "#             return -1\n",
    "        # return 0\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            if int(v1) > int(v2):\n",
    "                return 1\n",
    "            if int(v1) < int(v2):\n",
    "                return -1\n",
    "        return 0\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ver1_list=[int(i) for i in version1.split('.')]\n",
    "        ver2_list=[int(i) for i in version2.split('.')]\n",
    "        dif_len=len(ver1_list)-len(ver2_list)\n",
    "        if dif_len>0:\n",
    "            ver2_list=ver2_list+[0]*dif_len\n",
    "        if dif_len<0:\n",
    "            ver1_list=ver1_list+[0]*(-dif_len)\n",
    "        for i in range(len(ver1_list)):\n",
    "            if ver1_list[i]==ver2_list[i]:\n",
    "                continue\n",
    "            elif ver1_list[i]>ver2_list[i]:\n",
    "                return 1\n",
    "            else:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def compareVersion(self, version1, version2):\n",
    "        v1 = [int(x) for x in version1.split(\".\") if x]\n",
    "        v2 = [int(x) for x in version2.split(\".\") if x]\n",
    "        while v1 or v2:\n",
    "            n1 = v1.pop(0) if v1 else 0\n",
    "            n2 = v2.pop(0) if v2 else 0\n",
    "            if n1 > n2:\n",
    "                return 1\n",
    "            elif n1 < n2:\n",
    "                return -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        m,n = 0,0\n",
    "\n",
    "        while m<len(version1) or n<len(version2):\n",
    "            a =0\n",
    "            b = 0\n",
    "            while m< len(version1) and version1[m]!='.':\n",
    "                a =a *10+int(version1[m])\n",
    "                m+=1\n",
    "            m+=1\n",
    "            while n<len(version2) and version2[n]!='.':\n",
    "                b =b *10+int(version2[n])\n",
    "                n+=1\n",
    "            n+=1\n",
    "            if a!=b:return 1 if a>b else -1\n",
    "        return 0\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1,v2 in zip_longest(version1.split('.'),version2.split('.'),fillvalue=0):\n",
    "            x,y=int(v1),int(v2)\n",
    "            if x>y:\n",
    "                return 1\n",
    "            elif y>x:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "            m,n=len(version1),len(version2)\n",
    "            i=j=0\n",
    "            while i<m or j<n:\n",
    "                a=b=0\n",
    "                while i<m and version1[i]!='.':\n",
    "                    a=10*a+int(version1[i])\n",
    "                    i+=1\n",
    "                while j<n and version2[j]!='.':\n",
    "                    b=10*b + int(version2[j])\n",
    "                    j+=1\n",
    "                if a>b:\n",
    "                    return 1\n",
    "                elif b>a:\n",
    "                    return -1\n",
    "                i+=1\n",
    "                j+=1\n",
    "            return 0\n",
    "                \n",
    "                \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        i, j,m,n = 0 ,0,0,0\n",
    "\n",
    "        while m<len(version1) or n<len(version2):\n",
    "            a =0\n",
    "            b = 0\n",
    "            while m< len(version1) and version1[m]!='.':\n",
    "                a =a *10+int(version1[m])\n",
    "                m+=1\n",
    "            m+=1\n",
    "            while n<len(version2) and version2[n]!='.':\n",
    "                b =b *10+int(version2[n])\n",
    "                n+=1\n",
    "            n+=1\n",
    "            if a!=b:return 1 if a>b else -1\n",
    "        return 0\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        version1_list = version1.split(\".\")\n",
    "        version2_list = version2.split(\".\")\n",
    "        m = len(version1_list)\n",
    "        n = len(version2_list)\n",
    "        i = 0\n",
    "        while i < m and i < n:\n",
    "            n1 = int(version1_list[i])\n",
    "            n2 = int(version2_list[i])\n",
    "            if n1 < n2:\n",
    "                return -1\n",
    "            elif n1 > n2:\n",
    "                return 1\n",
    "            i += 1\n",
    "        if m < n:\n",
    "            while i < n:\n",
    "                n2s = int(version2_list[i])\n",
    "                if n2s > 0:\n",
    "                    return -1\n",
    "                i += 1\n",
    "        elif m > n:\n",
    "            while i < m:\n",
    "                n1s = int(version1_list[i])\n",
    "                if n1s > 0:\n",
    "                    return 1\n",
    "                i += 1\n",
    "        return 0\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1=version1.split(\".\")\n",
    "        v2=version2.split(\".\")\n",
    "        v1=[int(i) for i in v1]\n",
    "        v2=[int(i) for i in v2]\n",
    "        if len(v1)<len(v2):\n",
    "            v1+=[0]*(len(v2)-len(v1))\n",
    "        elif len(v1)>len(v2):\n",
    "            v2+=[0]*(len(v1)-len(v2))\n",
    "        l=len(v2)\n",
    "        for i in range(l):\n",
    "            if v1[i]<v2[i]:\n",
    "                return -1\n",
    "            elif v1[i]>v2[i]:\n",
    "                return 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        m, n = len(v1), len(v2)\n",
    "        i, j = 0, 0\n",
    "        while i < m or j < n:\n",
    "            a = int(v1[i]) if i < m else 0\n",
    "            b = int(v2[i]) if j < n else 0\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        index_min = min(len(v1),len(v2))\n",
    "        index_max = max(len(v1),len(v2))\n",
    "        for i in range (index_min):\n",
    "            if int(v1[i]) > int(v2[i]):\n",
    "                return 1\n",
    "            if int(v1[i]) < int(v2[i]):\n",
    "                return -1\n",
    "        if len(v1)>len(v2):\n",
    "            con = v1\n",
    "            res = 1\n",
    "        else:\n",
    "            con = v2\n",
    "            res = -1\n",
    "        for i in range(index_min,index_max):\n",
    "            if int(con[i])>0:\n",
    "                return res\n",
    "\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ls1 = list(map(lambda x:int(x),version1.split('.')))\n",
    "        ls2 = list(map(lambda x:int(x),version2.split('.')))\n",
    "\n",
    "        for i in range(max(len(ls1),len(ls2))):\n",
    "            v1 = ls1[i] if i <len(ls1) else 0\n",
    "            v2 = ls2[i] if i <len(ls2) else 0\n",
    "            if v1 > v2:\n",
    "                return 1\n",
    "            elif v1 < v2:\n",
    "                return -1\n",
    "        \n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1: List[str] = version1.split(\".\")\n",
    "        v2: List[str] = version2.split(\".\")\n",
    "        for x, y in zip_longest(v1, v2, fillvalue=0):\n",
    "            x, y = int(x), int(y)\n",
    "            if x!= y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ver1_list,ver2_list = version1.split('.'), version2.split('.')\n",
    "\n",
    "        for i in range(min(len(ver1_list), len(ver2_list))):\n",
    "            if int(ver1_list[i])>int(ver2_list[i]):\n",
    "                return 1\n",
    "            elif int(ver1_list[i])<int(ver2_list[i]):\n",
    "                return -1\n",
    "        if len(ver1_list)>len(ver2_list):\n",
    "            for j in range(i+1, len(ver1_list)):\n",
    "                if int(ver1_list[j])>0:\n",
    "                    return 1\n",
    "            return 0\n",
    "        elif len(ver1_list)<len(ver2_list):\n",
    "            for j in range(i+1, len(ver2_list)):\n",
    "                if int(ver2_list[j])>0:\n",
    "                    return -1\n",
    "            return 0\n",
    "        else:\n",
    "            return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from itertools import zip_longest\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        for v1, v2 in zip_longest(version1.split(\".\"), version2.split(\".\"), fillvalue=0):\n",
    "            #print(v1, v2)\n",
    "            x, y = int(v1), int(v2)\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1,v2 = list(map(int,version1.split('.'))),list(map(int,version2.split('.')))\n",
    "        l1,l2 = len(v1),len(v2)\n",
    "        if l1 < l2: v1 += [0] * (l2-l1)\n",
    "        else: v2 += [0] * (l1-l2)\n",
    "        return 1 if v1 > v2 else -1 if v1 < v2 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        s1 = [int(_) for _ in version1.split('.')]\n",
    "        s2 = [int(_) for _ in version2.split('.')]\n",
    "\n",
    "        if len(s1) > len(s2):\n",
    "            for i in range(len(s1)- len(s2)):\n",
    "                s2.append(0)\n",
    "        else:\n",
    "            for i in range(len(s2)- len(s1)):\n",
    "                s1.append(0)\n",
    "\n",
    "        for i in range(len(s1)):\n",
    "            if s1[i] > s2[i]:\n",
    "                return 1\n",
    "            if s1[i] < s2[i]:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        list1 = list(map(int,version1.split(\".\")))\n",
    "        list2 = list(map(int,version2.split(\".\")))\n",
    "        len1 = len(list1)\n",
    "        len2 = len(list2)\n",
    "        if len1>len2:\n",
    "            list2+=[0]*(len1-len2)\n",
    "        elif len1<len2:\n",
    "            list1+=[0]*(len2-len1)\n",
    "        # print(list1)\n",
    "        # print(list2)\n",
    "        for i in range(len(list1)):\n",
    "            if list1[i]>list2[i]:\n",
    "                return 1\n",
    "            elif list1[i]<list2[i]:\n",
    "                return -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split(\".\")\n",
    "        v2 = version2.split(\".\")\n",
    "\n",
    "        v1 = [int(s) for s in v1]\n",
    "        v2 = [int(s) for s in v2]\n",
    "\n",
    "        if len(v1) > len(v2):\n",
    "            v2 += [0] * (len(v1) - len(v2))\n",
    "        elif len(v1) < len(v2):\n",
    "            v1 += [0] * (len(v2) - len(v1))\n",
    "\n",
    "        print(v1)\n",
    "        print(v2)\n",
    "        for s1, s2 in zip(v1, v2):\n",
    "            if s1 < s2:\n",
    "                return -1\n",
    "            elif s1 > s2:\n",
    "                return 1\n",
    "    \n",
    "        return 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def compareVersion(self, version1, version2):\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        if len(v1) > len(v2):\n",
    "            v2 += ['0'] * (len(v1) - len(v2))\n",
    "        else:\n",
    "            v1 += ['0'] * (len(v2) - len(v1))\n",
    "        for i in range(len(v1)):\n",
    "            if int(v1[i]) != int(v2[i]):\n",
    "                return 1 if int(v1[i]) > int(v2[i]) else -1\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        m,n  = len(version1),len(version2)\n",
    "        i = j = 0\n",
    "        while i < m or j < n:\n",
    "            a = b = 0\n",
    "            while i < m and version1[i] != '.':\n",
    "                a = 10*a + int(version1[i])\n",
    "                i += 1\n",
    "            while j < n and version2[j] != '.':\n",
    "                b = 10*b + int(version2[j])\n",
    "                j += 1\n",
    "            if a > b:\n",
    "                return 1\n",
    "            elif a < b:\n",
    "                return -1\n",
    "            i += 1\n",
    "            j += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        n, m = len(version1), len(version2)\n",
    "        p1, p2 = 0, 0\n",
    "        while p1 < n or p2 < m:\n",
    "            num1 = 0\n",
    "            while p1 < n and version1[p1] != \".\":\n",
    "                num1 = num1 * 10 + ord(version1[p1]) - ord(\"0\")\n",
    "                p1 += 1\n",
    "            p1 += 1\n",
    "            num2 = 0\n",
    "            while p2 < m and version2[p2] != \".\":\n",
    "                num2 = num2 * 10 + ord(version2[p2]) - ord(\"0\")\n",
    "                p2 += 1\n",
    "            p2 += 1\n",
    "            if num1 > num2:\n",
    "                return 1\n",
    "            elif num1 < num2:\n",
    "                return -1\n",
    "        return 0\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1, v2 = [version1], [version2]\n",
    "        if version1.find('.')!=-1:\n",
    "            v1 = version1.split('.')\n",
    "        if version2.find('.')!=-1:\n",
    "            v2 = version2.split('.')\n",
    "        key = -1\n",
    "        if len(v1)>len(v2):\n",
    "            L = v1\n",
    "            S = v2\n",
    "            key = 1\n",
    "        else:\n",
    "            L = v2\n",
    "            S = v1\n",
    "\n",
    "        i = 0\n",
    "        while i < len(L):\n",
    "            if i < len(S):\n",
    "                if int(L[i]) > int(S[i]):\n",
    "                    return key\n",
    "                elif int(L[i]) < int(S[i]):\n",
    "                    return -key\n",
    "                else:\n",
    "                    i+=1\n",
    "                    continue\n",
    "            else:\n",
    "                if int(L[i])!=0:\n",
    "                    return key\n",
    "                i+=1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1_list = version1.split('.')\n",
    "        v2_list = version2.split('.')\n",
    "\n",
    "        m = len(v1_list)\n",
    "        n = len(v2_list)\n",
    "\n",
    "        i = 0\n",
    "        while i < m or i < n:\n",
    "            v1 = v1_list[i] if i < m else '0'\n",
    "            v2 = v2_list[i] if i < n else '0'\n",
    "\n",
    "\n",
    "            while len(v1) > 1 and v1[0] == '0':\n",
    "                v1 = v1[1:]\n",
    "            while len(v2) > 1 and v2[0] == '0':\n",
    "                v2 = v2[1:]\n",
    "            \n",
    "            if int(v1) > int(v2):\n",
    "                return 1\n",
    "            elif int(v1) < int(v2):\n",
    "                return -1\n",
    "\n",
    "            i += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        i,j=0,0\n",
    "        while i<len(version1) or j<len(version2):\n",
    "            num1,num2=0,0\n",
    "            while i<len(version1) and version1[i].isdigit():\n",
    "                num1=num1*10+int(version1[i])\n",
    "                i+=1\n",
    "            # num1=int(version1[s1:i])\n",
    "\n",
    "            while j<len(version2) and version2[j].isdigit():\n",
    "                num2=num2*10+int(version2[j])\n",
    "                j+=1\n",
    "            # num2=int(version2[s2:j])\n",
    "\n",
    "            if num1>num2:\n",
    "                return 1\n",
    "            elif num1<num2:\n",
    "                return -1\n",
    "            i+=1\n",
    "            j+=1\n",
    "\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        lst1 = version1.split('.')\n",
    "        lst2 = version2.split('.')\n",
    "        lst1 = list(map(int, lst1))\n",
    "        lst2 = list(map(int, lst2))\n",
    "        len1 = len(lst1)\n",
    "        len2 = len(lst2)\n",
    "        if len1<len2:\n",
    "            for _ in range(len2-len1):\n",
    "                lst1.append(0)\n",
    "        elif len1>len2:\n",
    "            for _ in range(len1-len2):\n",
    "                lst2.append(0)\n",
    "        len3 = len(lst1)\n",
    "        for i in range(len3):\n",
    "            if lst1[i]>lst2[i]:\n",
    "                return 1\n",
    "            elif lst1[i]<lst2[i]:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        arr_1 = list(map(int, version1.split(\".\")))\n",
    "        arr_2 = list(map(int, version2.split(\".\")))\n",
    "        # print(arr_1)\n",
    "        # print(arr_2)\n",
    "        result = 0\n",
    "\n",
    "        for i in range(min(len(arr_1), len(arr_2))):\n",
    "            if arr_1[i] > arr_2[i]:\n",
    "                result = 1\n",
    "                break\n",
    "            elif arr_1[i] < arr_2[i]:\n",
    "                result = -1\n",
    "                break\n",
    "            else:\n",
    "                continue\n",
    "\n",
    "        if result == 0:\n",
    "            if len(arr_1) > len(arr_2):\n",
    "                for j in range(len(arr_2), len(arr_1)):\n",
    "                    if arr_1[j] != 0:\n",
    "                        result = 1\n",
    "            if len(arr_2) > len(arr_1):\n",
    "                for j in range(len(arr_1), len(arr_2)):\n",
    "                    if arr_2[j] != 0:\n",
    "                        result = -1\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "\n",
    "        m = len(version1)\n",
    "        n = len(version2)\n",
    "        left = 0\n",
    "        right = 0\n",
    "\n",
    "        while left < m or right < n:\n",
    "\n",
    "            x = 0\n",
    "            while left < m and version1[left] != '.':\n",
    "                x = x * 10 + ord(version1[left]) - ord('0')\n",
    "                left += 1\n",
    "            left += 1\n",
    "\n",
    "            y = 0\n",
    "            while right < n and version2[right] != '.':\n",
    "                y = y * 10 + ord(version2[right]) - ord('0')\n",
    "                right += 1\n",
    "            right += 1\n",
    "\n",
    "            if x != y:\n",
    "                return 1 if x > y else -1\n",
    "\n",
    "        return 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        nums1 = version1.split(\".\")\n",
    "        nums2 = version2.split(\".\")\n",
    "        n1, n2 = len(nums1), len(nums2)\n",
    "        i, j = 0, 0\n",
    "        while i < n1 and j < n2:\n",
    "            if int(nums1[i]) < int(nums2[j]):\n",
    "                return -1\n",
    "            elif int(nums1[i]) > int(nums2[j]):\n",
    "                return 1\n",
    "            i, j = i + 1, j + 1\n",
    "        while i < n1:\n",
    "            if int(nums1[i]) > 0:\n",
    "                return 1\n",
    "            i += 1\n",
    "        while j < n2:\n",
    "            if int(nums2[j]) > 0:\n",
    "                return -1\n",
    "            j += 1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1, v2 = version1.split('.'), version2.split('.')\n",
    "        l1, l2 = len(v1), len(v2)\n",
    "        for i in range(max(l1, l2)):\n",
    "            part1 = int(v1[i]) if i < l1 else 0\n",
    "            part2 = int(v2[i]) if i < l2 else 0\n",
    "            if part1 == part2: continue\n",
    "            elif part1 > part2: return 1\n",
    "            else: return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1=list(map(int,version1.split('.')))\n",
    "        v2 = list(map(int, version2.split('.')))\n",
    "\n",
    "        len_diff=abs(len(v1)-len(v2))\n",
    "        if len(v1)<len(v2):\n",
    "            v1.extend([0]*len_diff)\n",
    "        else:\n",
    "            v2.extend([0]*len_diff)\n",
    "\n",
    "        for num1,num2 in zip(v1,v2):\n",
    "            if num1 < num2:\n",
    "                return -1\n",
    "            elif num1 >  num2:\n",
    "                return 1\n",
    "\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        def v2int(version: List[str]):\n",
    "            res = []\n",
    "            for i in range(len(version)):\n",
    "                if version[i] == '0':\n",
    "                    res.append('0')\n",
    "                elif version[i][0] == '0':  # '001', '01'\n",
    "                    res.append(str(int(version[i])))\n",
    "                else:\n",
    "                    res.append(version[i])\n",
    "            return res\n",
    "\n",
    "        res1 = v2int(version1.split('.'))\n",
    "        res2 = v2int(version2.split('.'))\n",
    "        l1, l2 = len(res1), len(res2)\n",
    "        if l1 > l2:\n",
    "            res2.extend(['0'] * (l1-l2))\n",
    "        elif l1 < l2:\n",
    "            res1.extend(['0'] * (l2-l1))\n",
    "        res1 = int(''.join(res1))\n",
    "        res2 = int(''.join(res2))\n",
    "        if res1 > res2:\n",
    "            return 1\n",
    "        elif res1 < res2:\n",
    "            return -1\n",
    "        else:\n",
    "            return 0\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1 = version1.split('.')\n",
    "        v2 = version2.split('.')\n",
    "        \n",
    "        for x, y in zip(v1,v2):\n",
    "            if (int(x) > int(y)):\n",
    "                return 1\n",
    "            elif (int(x) < int(y)):\n",
    "                return -1\n",
    "\n",
    "        n1, n2 = len(v1), len(v2)\n",
    "        if (n1 > n2):\n",
    "            for i in range(n2, n1):\n",
    "                if int(v1[i]) > 0:\n",
    "                    return 1\n",
    "        elif (n1 < n2):\n",
    "            for i in range(n1, n2):\n",
    "                if int(v2[i]) > 0:\n",
    "                    return -1\n",
    "        \n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ls1,ls2 = version1.split('.'),version2.split('.')\n",
    "        for i in range(max(len(ls1),len(ls2))):\n",
    "            v1 = ls1[i] if i < len(ls1) else 0\n",
    "            v2 = ls2[i] if i < len(ls2) else 0\n",
    "            if int(v1) > int(v2):\n",
    "                return 1\n",
    "            elif int(v1) < int(v2):\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        # i,j为两个版本号字符串的索引 用来遍历和标记是否比较结束\n",
    "        m, n, i, j = len(version1), len(version2), 0, 0\n",
    "        # 每次循环需要更新的量 每次修订号结束作为一次循环\n",
    "        while i < m or j < n:\n",
    "            # 初始化每次的修订号\n",
    "            nums1, nums2 = 0, 0\n",
    "            while i < m and version1[i] != '.':\n",
    "                # 则说明是数字\n",
    "                nums1 = 10 * nums1 + int(version1[i])\n",
    "                i += 1\n",
    "            # 这时i指向了'.'或者i检索完\n",
    "            # 为下一次的i做准备 因为版本号是有效的 故.后面一定是数字\n",
    "            if i < m and version1[i] == '.':\n",
    "                i += 1\n",
    "            while j < n and version2[j] != '.':\n",
    "                nums2 = 10 * nums2 + int(version2[j])\n",
    "                j += 1\n",
    "            # 这时指向了'.'或者j检索完\n",
    "            # 为下一次的j做准备 因为版本号是有效的 故.后面一定是数字\n",
    "            if j < n and version2[j] == '.':\n",
    "                j += 1\n",
    "            # 得到nums1 和 nums2 进行比较\n",
    "            if nums1 > nums2:\n",
    "                return 1\n",
    "            elif nums1 < nums2:\n",
    "                return -1\n",
    "            else:\n",
    "                # 相等时继续比较 直至二者都比较结束\n",
    "                if i == m and j == n:\n",
    "                    return 0\n",
    "            \n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        i1=0\n",
    "        i2=0\n",
    "        while i1<len(version1) or i2<len(version2):\n",
    "            cur_v1=0\n",
    "            cur_v2=0\n",
    "            while i1<len(version1) and version1[i1]!='.':\n",
    "                cur_v1=cur_v1*10+int(version1[i1])\n",
    "                i1+=1\n",
    "            i1+=1\n",
    "            while i2<len(version2) and version2[i2]!='.':\n",
    "                cur_v2=cur_v2*10+int(version2[i2])\n",
    "                i2+=1\n",
    "            i2+=1\n",
    "            print(version1,i1,cur_v1,version2,i2,cur_v2)\n",
    "            if cur_v1>cur_v2:\n",
    "                return 1\n",
    "            if cur_v1<cur_v2:\n",
    "                return -1\n",
    "        return 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        def preProcess(version):\n",
    "            v = version.split(\".\")\n",
    "            for idx in range(len(v)):\n",
    "                part = v[idx]\n",
    "                if len(part) > 1:\n",
    "                    i = 0\n",
    "                    while i < len(part) and part[i] == \"0\":\n",
    "                        i += 1\n",
    "                    v[idx] = part[i:] if i < len(part) else 0\n",
    "                v[idx] = int(v[idx])\n",
    "            return v\n",
    "        \n",
    "        def addTail(version, length):\n",
    "            len_v = len(version)\n",
    "            for i in range(length - len_v):\n",
    "                version.append(0)\n",
    "            return version\n",
    "        v1 = preProcess(version1)\n",
    "        v2 = preProcess(version2)\n",
    "        print(v1)\n",
    "        print(v2)\n",
    "        if len(v1) > len(v2):\n",
    "            v2 = addTail(v2, len(v1))\n",
    "        else:\n",
    "            v1 = addTail(v1, len(v2))\n",
    "        \n",
    "        for i in range(len(v1)):\n",
    "            if v1[i] > v2[i]:\n",
    "                return 1\n",
    "            elif v1[i] < v2[i]:\n",
    "                return -1\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        v1, v2 = list(map(int, version1.split('.'))), list(map(int, version2.split('.')))\n",
    "        l1, l2 = len(v1), len(v2)\n",
    "        if l1 < l2: v1 += [0] * (l2 - l1)\n",
    "        else:       v2 += [0] * (l1 - l2)\n",
    "        return 1 if v1 > v2 else -1 if v1 < v2 else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def compareVersion(self, version1: str, version2: str) -> int:\n",
    "        ver1=[]\n",
    "        ver2=[]\n",
    "        ver1_val = 0\n",
    "        for i in version1:\n",
    "            if i!=\".\":\n",
    "                ver1_val=ver1_val*10\n",
    "                ver1_val+=ord(i)-ord(\"0\")\n",
    "            else:\n",
    "                ver1.append(ver1_val)\n",
    "                ver1_val=0\n",
    "        ver1.append(ver1_val)\n",
    "        ver2_val = 0\n",
    "        for i in version2:\n",
    "            if i!=\".\":\n",
    "                ver2_val=ver2_val*10\n",
    "                ver2_val+=ord(i)-ord(\"0\")\n",
    "            else:\n",
    "                ver2.append(ver2_val)\n",
    "                ver2_val=0\n",
    "        ver2.append(ver2_val)\n",
    "        p=0\n",
    "        while True:\n",
    "            if p>=len(ver1) and p>=len(ver2):return 0\n",
    "            ver1p,ver2p=0,0\n",
    "            if p<len(ver1):\n",
    "                ver1p=ver1[p]\n",
    "            if p<len(ver2):\n",
    "                ver2p=ver2[p]\n",
    "            if ver1p>ver2p:return 1\n",
    "            elif ver1p<ver2p:return -1\n",
    "            p+=1\n",
    "        return 0\n",
    "            \n",
    "                "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
